﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace Infrastructure
{
    public class FormBase : Form
    {
        protected TextBox txtTitle;
        private Dictionary<Keys, Button> keyButtonDic = new Dictionary<Keys, Button>();
        protected FormBase()
        {
            PerformStyle();
            CreateFormTitle();

            Load += new EventHandler(FormBase_Load);
            this.KeyDown += new KeyEventHandler(FormBase_KeyDown);
        }

        protected virtual int GetFormWidth()
        {
            return 700;
        }

        private void CreateFormTitle()
        {
            this.txtTitle = new System.Windows.Forms.TextBox();
            // 
            // txtTitle
            // 
            this.txtTitle.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.txtTitle.BackColor = System.Drawing.SystemColors.Control;
            this.txtTitle.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.txtTitle.Font = new System.Drawing.Font("Microsoft YaHei", 25F);
            this.txtTitle.ForeColor = System.Drawing.SystemColors.WindowText;
            this.txtTitle.ImeMode = System.Windows.Forms.ImeMode.Disable;
            this.txtTitle.Location = new System.Drawing.Point(0, 38);
            this.txtTitle.Margin = new System.Windows.Forms.Padding(0);
            this.txtTitle.Name = "txtTitle";
            this.txtTitle.ReadOnly = true;
            this.txtTitle.Width = GetFormWidth();
            this.txtTitle.TabIndex = 0;
            this.txtTitle.TabStop = false;
            this.txtTitle.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.txtTitle.WordWrap = false;
        }

        private void PerformStyle()
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
            this.KeyPreview = true;
            this.MaximizeBox = false;
            this.Name = "FormBase";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.ControlBox = false;
        }

        protected virtual void CloseCurrentWindow()
        {
            this.Close();
        }

        protected virtual void HandleFKeyEvent()
        {
            foreach (Control control in this.Controls)
            {
                if (control.GetType() == typeof(Button))
                {
                    Button btn = (Button)control;
                    if (btn.Tag != null && btn.Tag.ToString().StartsWith("F"))
                    {
                        BindEventToFKey(btn, btn.Tag.ToString());
                    }
                }

                HandleFKeyEvent(control);
            }
        }

        private void HandleFKeyEvent(Control controlParent)
        {
            foreach (Control control in controlParent.Controls)
            {
                if (control.GetType() == typeof(Button))
                {
                    Button btn = (Button)control;
                    if (btn.Tag != null && btn.Tag.ToString().StartsWith("F"))
                    {
                        BindEventToFKey(btn, btn.Tag.ToString());
                    }
                }

                HandleFKeyEvent(control);
            }
        }

        private void BindEventToFKey(Button btn, string keyCode)
        {
            Keys key;
            if (!Enum.TryParse<Keys>(keyCode, true, out key)) return;

            keyButtonDic.Add(key, btn);
        }

        void FormBase_KeyDown(object sender, KeyEventArgs e)
        {
            Button relatedButton = null;
            if (!keyButtonDic.ContainsKey(e.KeyCode)) return;
            relatedButton = keyButtonDic[e.KeyCode];
            MethodInfo info = typeof(Button).GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
            if (info == null) return;
            info.Invoke(relatedButton, new object[] { EventArgs.Empty });
            e.Handled = true;
        }

        void FormBase_Load(object sender, EventArgs e)
        {
            HandleFKeyEvent();
        }
    }
}
